/*
 * Copyright (C) 2012 rothens
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package rpg.mouse;

import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.util.EnumMap;
import java.util.HashMap;
import rpg.Location;
import rpg.RPG;
import rpg.entities.Player;
import rpg.maps.Map;

/**
 *
 * @author rothens
 */
public class MListener implements MouseListener, MouseMotionListener {

    private static MListener instance = new MListener();
    public Location clickedTile = null;
    public EnumMap<MouseEventType, MouseEvent> mouseEvents = new EnumMap<MouseEventType, MouseEvent>(MouseEventType.class);
    public MouseEvent lastPosition = null;

    public MListener() {
    }

    @Override
    public void mouseClicked(MouseEvent me) {
        mouseEvents.put(MouseEventType.CLICK, me);
    }

    @Override
    public void mousePressed(MouseEvent me) {
        mouseEvents.put(MouseEventType.PRESS, me);
    }

    public Location convertToTileLoc(int x, int y) {
        int w = RPG.WINDOW_WIDTH;
        int h = RPG.WINDOW_HEIGHT;
        Location ret = null;
        try {
            int cX = x;
            int cY = y;
            Location loc = Player.getInstance().getCurrentLocation();
            Map map = loc.getMap();
            int pX = loc.getX();
            int pY = loc.getY();
            if (pX > w / 2 && pX < map.getWidth() * 64 - w / 2) {
                cX += pX - w / 2;
            }
            if (pX > map.getWidth() * 64 - w / 2) {
                cX += map.getWidth() * 64 - w;
            }
            if (pY > h / 2 && pY < map.getHeight() * 64 - h / 2) {
                cY += pY - h / 2;
            }
            if (pY > map.getHeight() * 64 - h / 2) {
                cY += map.getHeight() * 64 - h;
            }
            ret = new Location(cX, cY, false, loc.getMap());

        } catch (NullPointerException npe) {
        }
        return ret;
    }

    @Override
    public void mouseReleased(MouseEvent me) {
        mouseEvents.put(MouseEventType.RELEASE, me);
    }

    @Override
    public void mouseEntered(MouseEvent me) {
    }

    @Override
    public void mouseExited(MouseEvent me) {
    }

    public void tick() {
        mouseEvents.clear();
    }
    

    public static MListener getInstance() {
        return instance;
    }

    @Override
    public void mouseDragged(MouseEvent me) {
        lastPosition = me;
    }

    @Override
    public void mouseMoved(MouseEvent me) {
        mouseEvents.put(MouseEventType.MOVE, me);
        lastPosition = me;
    }

    public boolean isMouseClicked() {
        return mouseEvents.get(MouseEventType.CLICK) != null;
    }

    public boolean isMousePressed() {
        return mouseEvents.get(MouseEventType.PRESS) != null;
    }

    public boolean isMouseReleased() {
        return mouseEvents.get(MouseEventType.RELEASE) != null;
    }
}
